﻿using Module.Common.PubEvent;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Module.Common.Common.QiPuHelper
{
    public class FullTreeOfQiPu : BindableBase
    {
        private readonly IEventAggregator Ea;
        public string ModuleName { get; set; }
        public string FileName { get; set; }
        private TreeNodeOfQiPu _fullTreeQiPu;

        public TreeNodeOfQiPu FullTreeQiPu
        {
            get { return _fullTreeQiPu; }
            set
            {
                SetProperty(ref _fullTreeQiPu, value);

            }
        }
        private TreeNodeOfQiPu _cursorStep;
        public TreeNodeOfQiPu CursorStep
        {
            get { return _cursorStep; }
            set
            {
                if (_cursorStep != null) _cursorStep.IsSelected = false;
                SetProperty(ref _cursorStep, value);
                _cursorStep.IsSelected = true;
            }
        }
        //private TreeNodeOfQiPu CursorStep;

        public FullTreeOfQiPu(IEventAggregator ea, IContainerProvider containerProvider)
        {
            Ea = ea;
            FullTreeQiPu = containerProvider.Resolve<TreeNodeOfQiPu>();
            CursorStep = FullTreeQiPu;
            Ea.GetEvent<SystemMessageEvent>().Subscribe(SystemMessageProcess);
            Ea.GetEvent<ModuleMessageEvent>().Subscribe(ModuleMessageProcess, ThreadOption.PublisherThread, false, filter => filter.ModuleName.Equals(ModuleName));
        }

        private void SystemMessageProcess(SystemMessage obj)
        {
            switch (obj.MessageName)
            {
                
                default:
                    break;
            }
        }

        private void ModuleMessageProcess(ModuleMessage obj)
        {
            switch (obj.MessageName)
            {
                case "updateQiPu":
                    TreeNodeOfQiPu qipu = new();
                    qipu.SetStepData(obj.Id, obj.BufferCol, obj.BufferRow, obj.NewCol, obj.NewRow, obj.DieQz);
                    CursorStep = AddChild(qipu);
                    Ea.GetEvent<QiPuMessageEvent>().Publish(new QiPuMessage
                    {
                        MessageName = "fullTreeHasUpdate",
                        SenderFrom = FileName,
                        TreeofQipu = FullTreeQiPu,
                        ModuleName = ModuleName,
                        Content = $"【{this.GetType().Name}】：棋谱完全树已更新完毕，请依此更新棋谱收缩树"
                    });
                    break;
                case "goBackStep":
                    if (CursorStep.IsRoot()) break;
                    Ea.GetEvent<ModuleMessageEvent>().Publish(new ModuleMessage()
                    {
                        Id = CursorStep.QiZiId,
                        BufferCol = CursorStep.X0,
                        BufferRow = CursorStep.Y0,
                        NewCol = CursorStep.X1,
                        NewRow = CursorStep.Y1,
                        DieQz = CursorStep.DieQz,
                        MessageName = "goBackStepLunch",
                        SenderFrom = FileName,
                        Content = $"【{this.GetType().Name}】：棋谱指针已回退了一步，请棋子执行后退一步动作",
                        ModuleName = ModuleName

                    });
                    CursorStep = CursorStep.GetParent();
                    break;
                case "reStart": // 重新开局
                    FullTreeQiPu.Childrens.Clear();
                    CursorStep = FullTreeQiPu;
                    Ea.GetEvent<QiPuMessageEvent>().Publish(new QiPuMessage
                    {
                        MessageName = "fullTreeHasUpdate",
                        SenderFrom = this.GetType().Name,
                        TreeofQipu = FullTreeQiPu,
                        ModuleName = ModuleName,
                        Content = $"【{this.GetType().Name}】：棋谱完全树初始化，请初始化收缩树"
                    });
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 棋谱增加子节点，仅用于FullTree完全树，不能在收缩树中使用
        /// </summary>
        /// <param name="child"></param>
        /// <returns>返回增加的节点</returns>
        public TreeNodeOfQiPu AddChild(TreeNodeOfQiPu child)
        {
            foreach (var item in CursorStep.Childrens)
            {
                if (string.Equals(item.Cn, child.Cn, StringComparison.OrdinalIgnoreCase))
                {
                    return item;  // 如果已存在，则不增加新步骤
                }
            }
            //child.CurrentRecord = null;
            child.ParentNode = CursorStep;
            child.ID = CursorStep.ID + 1;
            CursorStep.Childrens.Add(child);
            return child;
        }
    }

   
}

